package extnode

//replication factor - #followers
const (
	FullReplFactor = 2
)

const (
	ErrLogFileName   = "extnode_err.log"
	WorkLogFileName  = "extnode_work.log"
	InfoLogFileName  = "extnode_info.log"
	WriteLogFileName = "extnode_write.log"
	ReadLogFileName  = "extnode_read.log"
	DelLogFileName   = "extnode_del.log"
)

//system parameters
const (
	MaxServerId          = (1 << 20)
	NormalWakeUpPeriod   = 5         //min
	AbnormalWakeUpPeriod = 5         //min
	WriteBlockingDelay   = 20        //ms
	ReconnectDelay       = 50        //ms
	DataSyncBufSize      = (1 << 20) //1MB
	MaxDiskErrors        = 100
	MaxCachedFd          = 10000
	MaxFollowerConnPair  = 500
)

//server state
const (
	DiskAlreadyFull   = 0
	ReplGroupReady    = 1
	ReplGroupSplit    = 2
	ReplGroupRecovery = 3
	ShuttingDown      = 4
	DiskCorrupted     = 5
)

func WeightFactor(space int64, state int32) int64 {
	if state == 1 {
		return space
	} else if state > 3 {
		return 0 - space
	}

	return 0
}

//note server is numbered as 1, 2, ...
func IsLeader(srvId int) bool {
	return srvId%(FullReplFactor+1) == 1
}

func GrpId(srvId int) int {
	return (srvId-1)/(FullReplFactor+1) + 1
}
